/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp;

import java.util.Set;

import android.app.ProgressDialog;
import android.os.Bundle;
import dk.redia.lib.log.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import dk.redia.lib.utils.AppApplication;
import dk.redia.lib.utils.AppConfiguration;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;
import dk.redia.lib.xmlrpc.XmlRpcResultDumper;
import dk.redia.libraryapp.activityutils.LibraryAppActivity;
import dk.redia.libraryapp.adapters.AdapterStateListener;
import dk.redia.libraryapp.adapters.MyReservationsAdapter;
import dk.redia.libraryapp.authentication.Authentication;
import dk.redia.libraryapp.model.Reservation;
import dk.redia.libraryapp.xmlrpc.library.XmlRpcClient;

public class MyReservationsActivity extends LibraryAppActivity implements AdapterStateListener {

	protected static final String logTag = "MyReservationsActivity";

	private MyReservationsAdapter adapter;
	private ProgressDialog progressDialog;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.myreservations);
	    adapter = new MyReservationsAdapter(this);
	    adapter.addAdapterSizeListener(this);
	    ListView lv = (ListView)findViewById(R.id.myReservationsListView);
	    lv.setAdapter(adapter);

/*	    LibraryModel.getInstance().getReservations(0, 20, new LibraryModelListener() {
	    	public void getReservationsResult(List<Reservation> reservations, int offset, int count, int totalCount) {
	    		Log.d(MyReservationsActivity.logTag, "Retrieved reservations");
	    		for (Reservation reservation : reservations) {
					Log.d(MyReservationsActivity.logTag, "Found reservation: " + reservation);
				}
	    	}
	    	public void getReservationsFailure(String message) {
	    		Log.e(MyReservationsActivity.logTag, "Failed to retrieve reservations: " + message);
	    	}
		});		
*/
//	    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.list_layout_controller);
//	    lv.setLayoutAnimation(controller);
	    ((TextView)findViewById(R.id.myReservationsUserNameTextView)).setText(Authentication.getInstance().getUserName());
	    progressDialog = ProgressDialog.show(
	    		this, 
	    		getString(R.string.myReservationsProgressTitleText), 
	    		getString(R.string.myReservationsProgressBodyText), 
	    		true);
	    adapter.init();
	    
//	    loadAll();
	}

	/*
	private void editing(boolean editing) {
		this.isEditing = editing;
		adapter.setSelecting(editing);
		adapter.clearSelection();
		//adapter.clearConfirmed();
		ListView reservationsListView = (ListView)findViewById(R.id.myReservationsListView);
		Button editButton = (Button)findViewById(R.id.myReservationsEditImageButton);
		Button okButton = (Button)findViewById(R.id.myReservationsOkImageButton);
		editButton.setVisibility(editing ? View.GONE : View.VISIBLE);
		okButton.setVisibility(editing ? View.VISIBLE : View.GONE);
		reservationsListView.setChoiceMode(editing ? ListView.CHOICE_MODE_MULTIPLE : ListView.CHOICE_MODE_NONE);
	}
	*/
	/*
	private void loading(boolean loading) {
		if (loading) {
			editing(false);
		}
		findViewById(R.id.myReservationsListView).setVisibility(loading ? View.GONE : View.VISIBLE);
		findViewById(R.id.myReservationsLoadingProgressBar).setVisibility(loading ? View.VISIBLE : View.GONE);
	}
	*/
	
	public void onSelectedCheckboxClicked(View v) {
		Log.d(logTag, "Checkbox clicked");
		CheckBox cb = (CheckBox)v;
		adapter.setSelected((Integer)v.getTag(), cb.isChecked());
		Button b = (Button)findViewById(R.id.myReservationsConfirmImageButton);
		b.setEnabled(adapter.getSelectedCount()>0);
	}
	
/*	public void onEditButtonClicked(View v) {
		Log.d(MyReservationsActivity.logTag, "Edit button clicked");
		adapter.setSelecting(true);
		v.setVisibility(View.GONE);
		findViewById(R.id.myReservationsOkImageButton).setVisibility(View.VISIBLE);
	}*/
	
	public void onConfirmButtonClicked(View v) {
		Set<Integer> selectedReservations = adapter.getSelected();
		String[] reservationIdsArr = new String[selectedReservations.size()];
		int i=0;
		for (Integer idx : selectedReservations) {
			Reservation reservation = (Reservation)adapter.getItem(idx);
			reservationIdsArr[i++] = reservation.getReservationId(); 
		}
	    progressDialog = ProgressDialog.show(
	    		this, 
	    		getString(R.string.myReservationsDeletingReservationsHeadline), 
	    		getString(R.string.myReservationsDeletingReservationsText), 
	    		true);
		XmlRpcClient.getInstance().removeReservations(AppConfiguration.getInstance().getCustomerId(), AppConfiguration.getInstance().getApiKey(), reservationIdsArr, new XmlRpcClientResultListener() {
			
			public void callFailed(String message) {
				Log.e(logTag, "Failed to remove reservations");
			}
			
			public void callCompleted(Object resultObj) {
				XmlRpcResultDumper.dump(resultObj);
				Log.d(logTag, "Successfully removed reservations");
				adapter.reset();
			}
		});
	}
	
	public void onOkButtonClicked(View v) {
		v.setVisibility(View.GONE);
		findViewById(R.id.myReservationsConfirmImageButton).setVisibility(View.VISIBLE);
		Log.d(MyReservationsActivity.logTag, "Ok button clicked");
		/*
		List<Integer> selectedReservations = adapter.getSelected();
		String[] reservationIdsArr = new String[selectedReservations.size()];
		for (int i=0;i<selectedReservations.size();i++) {
			Integer idx = selectedReservations.get(i);
			Reservation reservation = (Reservation)adapter.getItem(idx);
			reservationIdsArr[i] = reservation.getReservationId(); 
		}
		XmlRpcClient.getInstance().removeReservations(AppConfiguration.getInstance().getCustomerId(), AppConfiguration.getInstance().getApiKey(), reservationIdsArr, new XmlRpcClientResultListener() {
			
			public void callFailed(String message) {
				Log.e(logTag, "Failed to remove reservations");
			}
			
			public void callCompleted(Object resultObj) {
				XmlRpcResultDumper.dump(resultObj);
				Log.d(logTag, "Successfully removed reservations");
				adapter.reset();
			}
		});
		*/
	}
	
/*	public void onReservationItemClicked(View v) {
		Log.d(MyReservationsActivity.logTag, "Reservation list item clicked");
		if (isEditing) {
			Integer idx = (Integer)v.getTag();
			if (idx!=null) {
				adapter.setSelected(idx, true);
			}
		}
	}*/
/*	
	public void onDeleteButtonClicked(View v) {
		Log.d(MyReservationsActivity.logTag, "Confirmed deletion of reservation");
		//Reservation reservation = (Reservation)v.getTag();
		//adapter.toggleConfirmed(reservation.getReservationId());
		Integer idx = (Integer)v.getTag();
		if (idx!=null) {
			adapter.setSelected(idx, true);
		}
	}
	*/

	public void dataSetSizeChanged(int length) {
		TextView reservationsTextView = (TextView)findViewById(R.id.myReservationsReservationsTextView);
		String reservationsText = getResources().getString(R.string.myReservationsReservations);
		reservationsText += " (" + length + ")";
		reservationsTextView.setText(reservationsText);
	}

	public void chunkLoaded(int offset, int count) {
		progressDialog.dismiss();
	}

	public void chunkLoadFailed(int offset, int count, String message) {
		progressDialog.dismiss();
		AppApplication.toast(String.format(getString(R.string.chunkLoadFailedText), message), Toast.LENGTH_SHORT);
	}
	
}
